package com.yiren.dbaa.module.practice_set.map.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.constant.DbConstant;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.TrajectDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.map.contract.MapHistoryContract;
import com.yiren.dbaa.module.practice_set.map.contract.MapHomeContract;
import com.yiren.dbaa.module.practice_set.map.contract.MapReleaseContract;
import com.yiren.dbaa.module.practice_set.map.model.MapHistoryModel;
import com.yiren.dbaa.module.practice_set.map.model.MapHomeModel;
import com.yiren.dbaa.module.practice_set.map.model.MapReleaseModel;
import com.yiren.dbaa.module.practice_set.map.view.activity.MapCollectActivity;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/29 17:50
 * @description: 历史版本
 */
public class MapHistoryPresenter implements MapHistoryContract.Presenter {

    private Activity activity;
    private MapHistoryContract.Model mapHistoryModel;
    private MapHomeContract.Model mapHomeModel;
    private MapReleaseContract.Model mapReleaseModel;
    private UserParamConfigBean userParamConfig;

    public MapHistoryPresenter(Activity activity) {
        this.activity = activity;
        mapHistoryModel = new MapHistoryModel(activity);
        mapHomeModel = new MapHomeModel(activity);
        mapReleaseModel = new MapReleaseModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadVersionList(MapHistoryContract.View view, String userId, MapEntity map) {
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在加载数据..."));
        mapHistoryModel.getVersionList(userId, map.getSiteCode(), map.getName(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<MapEntity> mapList = (List<MapEntity>) result;
                view.showVersionList(mapList);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void downLoadMapData(MapEntity map, MapHistoryContract.View view) {
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在下载中..."));
        mapHomeModel.downloadSiteMap(map.getSiteCode(), userParamConfig.getUserInfo().getpUserId(), map.getMapVer(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                //本地数据插入
                if (UserDbController.getInstance(false).insertOrReplace(map)) {
                    //通知视图
                    LoadDialog.dismiss(activity);
                    MessageBox.MessageAndVoiceModel(activity, "下载地图成功");
                    activity.runOnUiThread(() -> {
                        CommonDialog.showTwoBtn(activity, "操作确认", "下载已完成，请问是直接发布，还是编辑地图？", "直接发布", "编辑地图", (dialog, which) -> {
                            if (which == -2) {
                                releaseMap(map);
                            } else {
                                DbConstant.TRAJECT_DATA_URL = DbConstant.App_Main_Directory + userParamConfig.getUserInfo().getId() + "/map/" + map.getSiteCode() + "/" + DbConstant.TRAJECT_DATA_NAME;
                                TrajectDbController.getInstance(true);
                                activity.startActivity(new Intent(activity, MapCollectActivity.class));
                                activity.finish();
                            }
                            dialog.dismiss();
                        });
                    });
                } else {
                    MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                }
            }

            @Override
            public void fail(String error) {
                super.fail(error);
            }
        });
    }

    @Override
    public void releaseMap(MapEntity map) {
        mapReleaseModel.publishMap(map, userParamConfig.getUserInfo().getId(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        map.setMapVer(map.getMapVerLast() + 1);
                        UserDbController.getInstance(false).update(map);
                        LoadDialog.dismiss(activity);
                        MessageBox.MessageAndVoiceModel(activity, "地图发布成功!");
                        activity.startActivity(new Intent(activity, PracticeCarSetActivity.class));
                    }
                });
            }

            @Override
            public void fail(String error) {
                activity.runOnUiThread(() -> CommonDialog.showOneBtn(activity, "操作提示", "地图数据发布失败!", "我知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }));
            }
        });
    }

}